package com.wzy.portfolio.utils;

import java.util.Random;

public class UserAgent {

    private final static Random random = new Random();

    // 浏览器版本
    private static final String[] CHROME_VERSIONS = {"120.0.0.0", "119.0.0.0", "118.0.0.0", "117.0.0.0"};
    private static final String[] FIREFOX_VERSIONS = {"121.0", "120.0", "119.0", "118.0"};
    private static final String[] SAFARI_VERSIONS = {"17.1", "17.0", "16.6", "16.5"};

    // 操作系统
    private static final String[] WINDOWS_VERSIONS = {"10.0", "11.0"};
    private static final String[] MAC_VERSIONS = {"10_15_7", "11_0_0", "12_0_0", "13_0_0"};

    /**
     * 生成随机Chrome User-Agent
     */
    public static String generateChromeUserAgent() {
        String os;
        String chromeVersion = CHROME_VERSIONS[random.nextInt(CHROME_VERSIONS.length)];

        if (random.nextBoolean()) {
            // Windows
            String windowsVersion = WINDOWS_VERSIONS[random.nextInt(WINDOWS_VERSIONS.length)];
            os = String.format("(Windows NT %s; Win64; x64)", windowsVersion);
        } else {
            // Mac
            String macVersion = MAC_VERSIONS[random.nextInt(MAC_VERSIONS.length)];
            os = String.format("(Macintosh; Intel Mac OS X %s)", macVersion);
        }

        return String.format("Mozilla/5.0 %s AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36",
                os, chromeVersion);
    }

    /**
     * 生成随机Firefox User-Agent
     */
    public static String generateFirefoxUserAgent() {
        String os;
        String firefoxVersion = FIREFOX_VERSIONS[random.nextInt(FIREFOX_VERSIONS.length)];

        if (random.nextBoolean()) {
            // Windows
            String windowsVersion = WINDOWS_VERSIONS[random.nextInt(WINDOWS_VERSIONS.length)];
            os = String.format("(Windows NT %s; Win64; x64; rv:%s)", windowsVersion, firefoxVersion);
        } else {
            // Mac
            String macVersion = MAC_VERSIONS[random.nextInt(MAC_VERSIONS.length)];
            os = String.format("(Macintosh; Intel Mac OS X %s; rv:%s)", macVersion, firefoxVersion);
        }

        return String.format("Mozilla/5.0 %s Gecko/20100101 Firefox/%s", os, firefoxVersion);
    }

    /**
     * 生成随机Safari User-Agent
     */
    public static String generateSafariUserAgent() {
        String macVersion = MAC_VERSIONS[random.nextInt(MAC_VERSIONS.length)];
        String safariVersion = SAFARI_VERSIONS[random.nextInt(SAFARI_VERSIONS.length)];

        return String.format("Mozilla/5.0 (Macintosh; Intel Mac OS X %s) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/%s Safari/605.1.15",
                macVersion, safariVersion);
    }

    /**
     * 生成随机Edge User-Agent
     */
    public static String generateEdgeUserAgent() {
        String windowsVersion = WINDOWS_VERSIONS[random.nextInt(WINDOWS_VERSIONS.length)];
        String chromeVersion = CHROME_VERSIONS[random.nextInt(CHROME_VERSIONS.length)];
        String edgeVersion = chromeVersion; // Edge版本通常与Chrome相近

        return String.format("Mozilla/5.0 (Windows NT %s; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36 Edg/%s",
                windowsVersion, chromeVersion, edgeVersion);
    }

    /**
     * 随机生成任意类型的User-Agent
     */
    public static String generateRandomUserAgent() {
        int type = random.nextInt(4);
        switch (type) {
            case 1: return generateFirefoxUserAgent();
            case 2: return generateSafariUserAgent();
            case 3: return generateEdgeUserAgent();
            case 0:
            default: return generateChromeUserAgent();
        }
    }
}